// TODO(tazjin): add sorted iterator when rangefuncs are stable (go 1.23?)
package value

import (
	"bytes"
	"context"
	"slices"
)

type NixAttrs map[String]NixValue

func (NixAttrs) NixType() string {
	return "set"
}

func (a NixAttrs) String() string {
	var buf bytes.Buffer
	buf.WriteString("{ ")

	keys := make([]String, 0, len(a))

	for k := range a {
		keys = append(keys, k)
	}

	slices.Sort(keys)

	for _, key := range keys {
		buf.WriteString(key.Identifier())
		buf.WriteString(" = ")
		buf.WriteString(a[key].String())
		buf.WriteString("; ")
	}

	buf.WriteByte('}')
	return buf.String()
}

func (a NixAttrs) Strict(ctx context.Context) error {
	var err error
	for _, v := range a {
		err = v.Strict(ctx)
		if err != nil {
			return err
		}
	}

	return nil
}
